/**
 * 把对象转字符串拼接起来
 * @param query
 * @returns
 */
export const querystring = (query: any): string => {
  if (["", null, undefined].includes(query)) {
    return "";
  }
  const keys = Object.keys(query);
  let result = "";
  keys.map((key) => {
    result += `&${key}=${query[key]}`;
  });
  const params = new URLSearchParams(result);
  return params.toString() || "";
};

/**
 * 不管怎么点击，只在500毫秒后触发事件，500毫秒内点击也要等到500毫秒后再触发事件
 * @param fn 需要防抖的函数
 * @param delay 毫秒
 * @returns
 */
export const debounce = (fn: Function, delay = 500) => {
  let timer: number | null = null;
  return (...args: any[]) => {
    if (timer) {
      clearTimeout(timer);
    }
    timer = window.setTimeout(() => {
      fn(...args);
    }, delay);
  }
}


// 格式化时间戳
export const formatDate =  (date: any):string => {
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  var d = date.getDate();
  d = d < 10 ? "0" + d : d;
  var h = date.getHours();
  var minute = date.getMinutes();
  minute = minute < 10 ? "0" + minute : minute;
  var second = date.getSeconds();
  second = minute < 10 ? "0" + second : second;
  return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
};

//获取图片函数
export const getIcon = (name: string): string => {
  return new URL(`../assets/${name}`, import.meta.url).href;
};
